package com.addplus.townmall.server.web.provider.serviceimpl.authoritymodule;

import com.addplus.townmall.server.api.constant.ErrorCode;
import com.addplus.townmall.server.api.constant.StringConstant;
import com.addplus.townmall.server.api.exception.ErrorException;
import com.addplus.townmall.server.api.mapper.authority.RoleMenuFunctionMapper;
import com.addplus.townmall.server.api.model.authority.RoleMenuFunction;
import com.addplus.townmall.server.api.model.authority.ext.RoleMenuFunctionExt;
import com.addplus.townmall.server.api.service.web.authoritymodule.RoleMenuFunctionService;
import com.addplus.townmall.server.api.utils.DataUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.dubbo.rpc.RpcContext;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 类名: RoleMenufunctionService
 *
 * @author zhangjiehang
 * @version V1.0
 * @date 2017/12/11 下午1:48
 * @description 类描述: 角色-功能关联实现类
 */
@Service(interfaceClass = RoleMenuFunctionService.class)
public class RoleMenuFunctionServiceImpl implements RoleMenuFunctionService {

    @Autowired
    private RoleMenuFunctionMapper roleMenufunctionMapper;


    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<RoleMenuFunction> getRoleMenuByRoleId(Integer id) throws Exception {
        if(DataUtils.EmptyOrNegativeOrZero(id)){
            throw new ErrorException(ErrorCode.SYS_ERROR_PARAM);
        }
        RoleMenuFunction roleMenu = new RoleMenuFunction();
        roleMenu.setrId(id);
        roleMenu.setIsDelete(0);
        List <RoleMenuFunction> roleMenufunctionList = roleMenufunctionMapper.selectList(new QueryWrapper<RoleMenuFunction>(roleMenu));
        if(roleMenufunctionList==null){
            throw new ErrorException(ErrorCode.SYS_ERROR_NULLDATE);
        }

        return roleMenufunctionList;
    }


    @Override
    public List<RoleMenuFunction> getRoleMenuByRoleAndMenuId(Integer menuId, Integer roleId) throws Exception {
        if(DataUtils.EmptyOrNegativeOrZero(roleId, menuId)){
            throw new ErrorException(ErrorCode.SYS_ERROR_PARAM);
        }
        RoleMenuFunction roleMenu = new RoleMenuFunction();
        roleMenu.setrId(roleId);
        roleMenu.setmId(menuId);
        roleMenu.setIsDelete(0);
        List <RoleMenuFunction> roleMenufunctionList = roleMenufunctionMapper.selectList(new QueryWrapper<RoleMenuFunction>(roleMenu));
        if(roleMenufunctionList==null){
            throw new ErrorException(ErrorCode.SYS_ERROR_NULLDATE);
        }

        return roleMenufunctionList;
    }

    @Override
    public Boolean addRoleMenufunction(RoleMenuFunction roleMenufunction)throws Exception {
        if(roleMenufunction ==null|| DataUtils.EmptyOrNegativeOrZero(roleMenufunction.getmId(),roleMenufunction.getrId())) {
            throw new ErrorException(ErrorCode.SYS_ERROR_PARAM);
        }
        roleMenufunction.setIsDelete(0);
        Date date = new Date();
        roleMenufunction.setGmtCreate(date);
        roleMenufunction.setGmtModified(date);
        int modifyUser= Integer.valueOf(RpcContext.getContext().getAttachment("id"));
        roleMenufunction.setModifyUser(modifyUser);
        int count = roleMenufunctionMapper.insert(roleMenufunction);
        if(count>0){
            //更新内容shrio里面内容
            redisTemplate.convertAndSend(StringConstant.REDIS_SHRIO_PERMISSION_TPOIC,StringConstant.REDIS_SHRIO_PERMISSION_TPOIC);
            return true;
        }else{
            throw new ErrorException(ErrorCode.SYS_ERROR_DATABASEFAIL,false);
        }
    }

    @Override
    public Boolean logicalDeletedRoleMenufunctionById(Integer id)throws Exception{
        if(DataUtils.EmptyOrNegativeOrZero(id)){
            throw new ErrorException(ErrorCode.SYS_ERROR_PARAM);
        }
        RoleMenuFunction roleMenufunction = new RoleMenuFunction();
        roleMenufunction.setId(id);
        roleMenufunction.setIsDelete(0);
        int count =roleMenufunctionMapper.selectCount(new QueryWrapper<RoleMenuFunction>(roleMenufunction));
        if(count>0){
            roleMenufunction.setIsDelete(1);
            roleMenufunction.setGmtModified(new Date());
            int modifyUser= Integer.valueOf(RpcContext.getContext().getAttachment("id"));
            roleMenufunction.setModifyUser(modifyUser);
            count = roleMenufunctionMapper.updateById(roleMenufunction);
            if(count>0){
                //更新内容shrio里面内容
                redisTemplate.convertAndSend(StringConstant.REDIS_SHRIO_PERMISSION_TPOIC,StringConstant.REDIS_SHRIO_PERMISSION_TPOIC);
                return true;
            }else{
                throw new ErrorException(ErrorCode.SYS_ERROR_DATABASEFAIL,false);
            }
        }else{
            throw new ErrorException(ErrorCode.SYS_ERROR_NOT_AVAILABLE);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveRoleMenuFunction(RoleMenuFunctionExt roleMenuFunctionExt) throws Exception {
        if (roleMenuFunctionExt == null) {
            throw new ErrorException(ErrorCode.SYS_ERROR_PARAM);
        }
        if (DataUtils.EmptyOrNegativeOrZero(roleMenuFunctionExt.getRoleId())) {
            throw new ErrorException(ErrorCode.SYS_ERROR_PARAM);
        }
        if (roleMenuFunctionExt.getmId() != null && roleMenuFunctionExt.getmId().length > 0) {
            List<RoleMenuFunction> roleMenuFunctionList = getRoleMenuByRoleId(roleMenuFunctionExt.getRoleId());
            List<Integer> addList = new ArrayList<>();
            List<Integer> delList = new ArrayList<>();
            for (Integer mId : roleMenuFunctionExt.getmId()) {
                boolean res = true;
                for (RoleMenuFunction r : roleMenuFunctionList) {
                    if (mId.equals(r.getmId())) {
                        res = false;
                    }
                }
                if (res) {
                    addList.add(mId);
                }
            }
            for (RoleMenuFunction rm : roleMenuFunctionList) {
                boolean res = true;
                for (Integer m : roleMenuFunctionExt.getmId()) {
                    if (rm.getmId().equals(m)) {
                        res = false;
                    }
                }
                if (res) {
                    delList.add(rm.getmId());
                }
            }
            int addRes = 0;
            int delRes = 0;
            if (delList.size() > 0) {
                addRes = roleMenufunctionMapper.batchDelete(roleMenuFunctionExt.getRoleId(), delList);
                if (addRes <= 0) {
                    throw new ErrorException(ErrorCode.SYS_ERROR_DATABASEFAIL);
                }
            }
            if (addList.size() > 0) {
                delRes = roleMenufunctionMapper.batchInsert(roleMenuFunctionExt.getRoleId(), addList);
                if (delRes <= 0) {
                    throw new ErrorException(ErrorCode.SYS_ERROR_DATABASEFAIL);
                }
            }
            if (addRes > 0 || delRes > 0) {
                //更新内容shrio里面内容
                redisTemplate.convertAndSend(StringConstant.REDIS_SHRIO_PERMISSION_TPOIC, StringConstant.REDIS_SHRIO_PERMISSION_TPOIC);
            }
        }
        return true;
    }
}
